const path = require("path");
const Joi = require("@hapi/joi");
const handler = require(path.join(__dirname, "../services/recordService.js"));
const { validate } = require("../middlewares/validate");

module.exports = (router, base) => {
  router.get(
    `/api/${base}/getRecordList`,
    validate({
      query: Joi.object().keys({
        page: Joi.number().optional().description("页码"),
        pageSize: Joi.number().optional().description("页数"),
        search: Joi.string().allow("").optional().description("搜索内容"),
        created: Joi.number().optional().description("排序方式"),
      }),
    }),
    handler.getRecordList
  );

  router.post(
    `/api/${base}/addRecord`,
    validate({
      payload: Joi.object().keys({
        amount: Joi.number().required().description("金额"),
        tag: Joi.string().required().description("分类的id"),
        user: Joi.string().required().description("用户的id"),
        detail: Joi.string().required().description("明细"),
        created: Joi.string().required().description("记录日期"),
      }),
    }),
    handler.addRecord
  );

  router.post(
    `/api/${base}/editRecord`,
    validate({
      payload: Joi.object().keys({
        id: Joi.string().required().description("标签id"),
        amount: Joi.number().required().description("金额"),
        tag: Joi.string().required().description("分类的id"),
        user: Joi.string().required().description("用户的id"),
        detail: Joi.string().required().description("明细"),
        created: Joi.string().required().description("记录日期"),
      }),
    }),
    handler.editRecord
  );

  router.post(
    `/api/${base}/deleteRecord`,
    validate({
      payload: Joi.object().keys({
        ids: Joi.array()
          .items(Joi.string().optional())
          .description("要删除的记录id集合"),
      }),
    }),
    handler.deleteRecord
  );

  router.get(`/api/${base}/getTagsAndUsers`, handler.getTagsAndUsers);
};
